//
//  PPMeInfoSexController.m
//  PropertyManage
//
//  Created by Dreamsoul on 15-3-26.
//  Copyright (c) 2015年 com.lab603.mc. All rights reserved.
//

#import "PPMeInfoSexController.h"
#import "PPMeInfoSexCellFrame.h"
#import "PPMeInfoSexCell.h"
#import "AFNetworking.h"
#import "MBProgressHUD+MJ.h"
#import "PPMe.h"
#import "EGOCache.h"
#import "UserInfo.h"
#define Cache_UserInfoKey @"UserInfoCache"
#define CacheTime 24*60*60
@interface PPMeInfoSexController ()
@property (nonatomic,strong) NSMutableArray *sexArray;
//本地保存的用户模型
@property (nonatomic,strong) UserInfo *userInfo;
@end

@implementation PPMeInfoSexController
/**
 *  用户模型的懒加载
 */
- (UserInfo *)userInfo{
    if (_userInfo==nil) {
        _userInfo = (UserInfo *)[[EGOCache globalCache] objectForKey:Cache_UserInfoKey];
    }
    return _userInfo;
}
- (NSMutableArray *)sexArray{
    if (_sexArray==nil) {
        _sexArray = [NSMutableArray array];
        PPMe *sexManMe = [[PPMe alloc] init];
        sexManMe.titleString = @"男";
        sexManMe.sexFlag = 0;
        sexManMe.sexIcon = @"new_feature_share_true";
        PPMe *sexWomanMe = [[PPMe alloc] init];
        sexWomanMe.titleString = @"女";
        sexWomanMe.sexFlag = 0;
        sexWomanMe.sexIcon = @"new_feature_share_true";
        NSString *sex = self.userInfo.sex;
        if ([sex isEqualToString:@"男"]) {
            sexManMe.sexFlag = 1;
        }else if([sex isEqualToString:@"女"]){
            sexWomanMe.sexFlag = 1;
        }
        PPMeInfoSexCellFrame *sexManMeF = [[PPMeInfoSexCellFrame alloc] init];
        sexManMeF.me = sexManMe;
        [_sexArray addObject:sexManMeF];
        PPMeInfoSexCellFrame *sexWomanMeF = [[PPMeInfoSexCellFrame alloc] init];
        sexWomanMeF.me = sexWomanMe;
        [_sexArray addObject:sexWomanMeF];
        
        }
    return _sexArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = IWColor(239, 239, 239);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.title = @"性别";
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.sexArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PPMeInfoSexCell *cell = [PPMeInfoSexCell cellWithTableView:tableView];
    PPMeInfoSexCellFrame *infoSexCellF = self.sexArray[indexPath.row];
    cell.infoSexF = infoSexCellF;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    PPMeInfoSexCellFrame *infoSexCellF = self.sexArray[indexPath.row];
    //上传数据
    NSString *urlString = [NSString stringWithFormat:@"%@/%@",BaseUrl,@"user/edit"];
    // AFNetworking\AFN
    // 1.创建请求管理对象
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    // 说明服务器返回的JSON数据
    mgr.responseSerializer = [AFJSONResponseSerializer serializer];
    //2.封装请求参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"userId"] = self.userInfo.ID;
    params[@"sex"] = infoSexCellF.me.titleString;
    //3.发送请求
    [mgr POST:urlString parameters:params success:^(AFHTTPRequestOperation *operation, NSDictionary* responseObject) {
        IWLog(@"-----%@------",responseObject);
        if ([responseObject[@"resultCode"] intValue]==1) {
            //保存数据
            self.userInfo.sex = params[@"sex"];
            [[EGOCache globalCache] setObject:self.userInfo forKey:Cache_UserInfoKey withTimeoutInterval:CacheTime];
            //向代理对象发送消息，更新数据
            //向block发送消息，更新数据
            if (self.userSexRefreshBlock) {
                self.userSexRefreshBlock(self.userInfo);
            }
            // 返回控制器
            [self.navigationController popViewControllerAnimated:YES];
            [MBProgressHUD showSuccess:@"修改成功"];
        }else{
            [MBProgressHUD showError:@"修改失败"];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD showError:@"修改失败"];
    }];

}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    PPMeInfoSexCellFrame *infoSexF = self.sexArray[indexPath.row];
    return infoSexF.cellHeight;
}
@end
